package com.ews.onlineexchange.util;

import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XmlUtil {

//	    /**
//	     * @description 将xml字符串转换成map
//	     * @param xml
//	     * @return Map
//	     */
//	    public static Map readStringXmlOut(String xml) {
//	        Map map = new HashMap();
//	        Document doc = null;
//	        try {
//	            doc = DocumentHelper.parseText(xml); // 将字符串转为XML
//	            Element rootElt = doc.getRootElement(); 
//	            Iterator iter = rootElt.elementIterator("head"); 
//	            while (iter.hasNext()) {
//	                Element recordEle = (Element) iter.next();
//	                String error_code = recordEle.elementTextTrim("error_code"); 
//	                map.put("error_code", error_code);
//	                String error_msg = recordEle.elementTextTrim("error_msg");
//	                map.put("error_msg", error_msg);
//	            }
//	        } catch (DocumentException e) {
//	            e.printStackTrace();
//	        } catch (Exception e) {
//	            e.printStackTrace();
//	        }
//	        return map;
//	    }
//	   
	 public static Map loadMapFromString(String xml)
	    {
	    	Document document = null;
	    	try {
	    	DocumentBuilderFactory builderFactory = DocumentBuilderFactory
	    	.newInstance();
	    	DocumentBuilder builder = builderFactory.newDocumentBuilder();
	    	document = builder.parse(new ByteArrayInputStream(xml
	    	.getBytes("utf-8")));
	    	} catch (Exception e) {
	    	e.printStackTrace();
	    	
	    	}
	    	Map feedback = new HashMap<>();
	    	NodeList error_code = document.getElementsByTagName("resultcode"); 
	    	NodeList error_msg = document.getElementsByTagName("errordescription"); 
	    	
	    	
		 	if(null != error_code.item(0).getChildNodes().item(0)) {
			    feedback.put("resultcode", error_code.item(0).getChildNodes().item(0).getNodeValue());
			}else {
				feedback.put("resultcode", "");
		    }
	    	
	    	if(null != error_msg.item(0).getChildNodes().item(0) && null != error_msg.item(0).getChildNodes().item(0).getNodeValue()) {
	    		feedback.put("errordescription", error_msg.item(0).getChildNodes().item(0).getNodeValue());
	    		
	    	}else {
	    		feedback.put("errordescription","");
	    	}
	    	return feedback;
	    }
	    
	    public static void main(String[] args) {
	        
//	        String myXml = "<?xml version='1.0' encoding='UTF-8'?>"+
//	        		"<xml><head><error_code>000000</error_code><error_msg></error_msg></head></xml>";
	        String myXml = "<?xml version='1.0' encoding='UTF-8'?>"+
	        "<result>"+
	        "<resultcode>0</resultcode>"+
	        "<taskcode>20130213231023</taskcode>"+
	        "<errordescription>发送成功</errordescription>"+
	        "</result>";

	        try {
				System.out.println(loadMapFromString(myXml));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
//	        Map map = readStringXmlOut(myXml);
//	        Iterator iters = map.keySet().iterator();
//	        while (iters.hasNext()) {
//	            String key = iters.next().toString(); // 拿到键
//	            String val = map.get(key).toString(); // 拿到值
//	            System.out.println(key + "=" + val);
//	        }
	    }
}